package cn.labbook.bluetooth.controller;

import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.entity.Admin;
import cn.labbook.bluetooth.entity.Constant;
import cn.labbook.bluetooth.service.IAdminService;
import cn.labbook.bluetooth.service.IConstantService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lonzy
 * @since 2019-08-29
 */
@Controller
@RequestMapping("/constant")
public class ConstantController extends AbstractController{

    @Autowired
    IConstantService constantService;
    @Autowired
    IAdminService adminService;


    @RequestMapping("setServerPrive")
    @ResponseBody
    public ResultDto setServerPrive(String serverPrive){
        LambdaQueryWrapper<Constant> lambda = new QueryWrapper<Constant>().lambda();
        lambda.eq(Constant::getK,"serverPrice");
        Constant constant = constantService.getOne(lambda);

        LambdaQueryWrapper<Admin> lambda1 = new QueryWrapper<Admin>().lambda();
        lambda1.eq(Admin::getPayRate,constant.getV());
        List<Admin> list = adminService.list(lambda1);
        list.forEach(admin -> {
            admin.setPayRate(Double.parseDouble(serverPrive));
        });
        adminService.updateBatchById(list);

        constant.setK("serverPrice");
        constant.setV(Double.valueOf(serverPrive));
        return constantService.updateById(constant)?success():fail();
    }

    @RequestMapping("get/payPrice")
    @ResponseBody
    public ResultDto getServerPrive(){
        LambdaQueryWrapper<Constant> lambda = new QueryWrapper<Constant>().lambda();
        lambda.eq(Constant::getK,"serverPrice");
        Constant constant = constantService.getOne(lambda);
        return constant!=null?success(constant.getV()):fail();
    }
}
